//============================================================
// Producnt name:		Shy
// Version: 			1.0
// Coded by:			Shy (465276976@qq.com)
// Auto generated at: 	2016/10/9 10:22:04
//============================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace Cloud.DAL
{

    public static class DBHelper
    {
        private static string ConnectionStrings = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;

        private static void PrepareExecute(string cmdText, SqlConnection conn, SqlCommand cmd, CommandType cmdType, SqlParameter[] pars)
        {
            PrepareExecute(cmdText, conn, cmd, cmdType, pars, null);
        }

        private static void PrepareExecute(string cmdText, SqlConnection conn, SqlCommand cmd, CommandType cmdType, SqlParameter[] pars, SqlTransaction tran)
        {
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            cmd.CommandType = cmdType;
            if (tran != null)
            {
                cmd.Transaction = tran;
            }
            if (pars != null)
            {
                cmd.Parameters.AddRange(pars);
            }
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            
        }

        public static SqlDataReader ExecuteQuery(string cmdText, CommandType cmdType, SqlParameter[] pars)
        {
            SqlConnection conn = new SqlConnection(ConnectionStrings);
            SqlCommand cmd = new SqlCommand();
            try
            {
                PrepareExecute(cmdText, conn, cmd, cmdType, pars);
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return reader;
            }
            catch (Exception ex)
            {
                conn.Close();
                throw new Exception(ex.Message);
            }
            finally
            {
                cmd.Parameters.Clear();
            }
            
            
        }


        public static object ExecuteScalar(string cmdText, CommandType cmdType, SqlParameter[] pars)
        {
            object result = null;
            using (SqlConnection conn = new SqlConnection(ConnectionStrings))
            {
                SqlCommand cmd = new SqlCommand();
                PrepareExecute(cmdText, conn, cmd, cmdType, pars);
                result = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
            }
            return result;
        }
        

        public static int ExecuteNonQuery(string cmdText, CommandType cmdType, SqlParameter[] pars)
        {
            int result = 0;
            using (SqlConnection conn = new SqlConnection(ConnectionStrings))
            {
                SqlCommand cmd = new SqlCommand();
                PrepareExecute(cmdText, conn, cmd, cmdType, pars);
                result = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
            }   
            return result;
        }


        public static int ExecuteNonQueryByTransaction(string cmdText, CommandType cmdType, SqlParameter[] pars)
        {
                 SqlConnection conn = new SqlConnection(ConnectionStrings);
                 conn.Open();
                 SqlTransaction tran = conn.BeginTransaction(IsolationLevel.ReadCommitted);
                 int result = 0;
                 SqlCommand cmd = new SqlCommand();
                 PrepareExecute(cmdText, conn, cmd, cmdType, pars, tran);
                  try
                  {
                      result = cmd.ExecuteNonQuery();
                      tran.Commit();
                   }
                  catch (Exception ex)
                  {
                       tran.Rollback();
                       throw new Exception(ex.Message);
                  }
                  finally
                 {
                    conn.Close();
                    cmd.Parameters.Clear();
                 }

                return result;
        }

        public static DataSet GetDataSet(string cmdText, CommandType cmdType, SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(ConnectionStrings))
            {
                SqlCommand cmd = new SqlCommand();
                PrepareExecute(cmdText, conn, cmd, cmdType, pars);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
        }


    }
}

